package com.eric.demo.netty.loopsend;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;

import java.util.Date;

/**
 * @author ZhangGuangyin on 2018/10/15
 */
public class TimeClientHandler extends ChannelHandlerAdapter {

    @Override

    public void channelRead(ChannelHandlerContext ctx, Object msg) {

        ByteBuf m = (ByteBuf) msg;

        try {

            long currentTimeMillis = (m.readUnsignedInt() - 2208988800L) * 1000L;

            System.out.println(new Date(currentTimeMillis));

        } finally {

            m.release();

        }

    }


    @Override

    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {

        cause.printStackTrace();

        ctx.close();

    }
}
